package com.example.myapplication;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.List;

public class LyricAdapter extends RecyclerView.Adapter<LyricAdapter.LyricViewHolder> {

    private List<MusicFragment.LyricLine> lyricLines;
    private int currentHighlightedPosition = -1;

    public LyricAdapter(List<MusicFragment.LyricLine> lyricLines) {
        this.lyricLines = lyricLines;
    }

    @NonNull
    @Override
    public LyricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lyric_item, parent, false);
        return new LyricViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LyricViewHolder holder, int position) {
        holder.lyricTextView.setText(lyricLines.get(position).lyric);

        if (position == currentHighlightedPosition) {
            holder.lyricTextView.setTextColor(Color.parseColor("#FF4081")); // Highlight color
            holder.lyricTextView.setTextSize(18); // Slightly larger font
        } else {
            holder.lyricTextView.setTextColor(Color.DKGRAY);
            holder.lyricTextView.setTextSize(16); // Normal font size
        }
    }

    @Override
    public int getItemCount() {
        return lyricLines.size();
    }

    public void highlightLyric(int position) {
        if (currentHighlightedPosition != position) {
            notifyItemChanged(currentHighlightedPosition); // Unhighlight previous
            currentHighlightedPosition = position;
            notifyItemChanged(currentHighlightedPosition); // Highlight new
        }
    }

    public void setLyrics(List<MusicFragment.LyricLine> newLyrics) {
        this.lyricLines.clear();
        this.lyricLines.addAll(newLyrics);
        currentHighlightedPosition = -1;
        notifyDataSetChanged();
    }

    public static class LyricViewHolder extends RecyclerView.ViewHolder {
        TextView lyricTextView;

        public LyricViewHolder(@NonNull View itemView) {
            super(itemView);
            lyricTextView = itemView.findViewById(R.id.text_view_lyric_line);
        }
    }
}
